package com.tanhua.server.service;

import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.vo.AnnouncementVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.AnnouncementApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 消息--公告
 */
@Service
public class AnnounService {

    // 订阅 AnnouncementApi
    @Reference
    private AnnouncementApi announcementApi;

    /**
     * 公告
     * @param page     当前页
     * @param pagesize 每页条数
     * @return
     */
    public ResponseEntity announcements(Long page, Long pagesize) {
        //1.调用api查询分页数据
        PageResult<Announcement> pageResult = announcementApi.Pageannouncements(page, pagesize);
        //2.取出查到数据里面的结果集，把它转成vo
        List<Announcement> Announcements = pageResult.getItems();
        //3.构建一个vos集合用存储vo
        List<AnnouncementVo> vos = new ArrayList<>();
        //4.遍历 pageResult 取出里面的每一条数据
        for (Announcement Announcement : Announcements) {
            //构建一个vo
            AnnouncementVo vo = new AnnouncementVo();
            //拷贝
            BeanUtils.copyProperties(Announcement, vo);
            //TODO 这里为啥要自己new一个时间
            if (Announcement.getCreated() != null) {
                vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            }
            //把得到的每一条vo收集起来
            vos.add(vo);
        }
        //5.构一个全新PageResult里面的集合装 vo 数据
        PageResult pageResultVo = new PageResult(pageResult.getCounts(),pageResult.getPagesize(),pageResult.getPages(),pageResult.getPage(),vos);

        //返回
        return ResponseEntity.ok(pageResultVo);
    }


}





















